package com.yu.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.yu.crm.domain.dto.NodeInfoDto;
import com.yu.crm.domain.pojo.CrmAreaNodeInfo;
import com.yu.crm.domain.request.AreaImageReq;
import com.yu.crm.domain.request.Punctuation;
import com.yu.crm.mapper.CrmAreaNodeInfoMapper;
import com.yu.crm.service.ICrmAreaNodeInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxzz.common.config.file.FastDFSClientWrapper;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author pan
 * @since 2020-07-22
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CrmAreaNodeInfoService extends ServiceImpl<CrmAreaNodeInfoMapper, CrmAreaNodeInfo> implements ICrmAreaNodeInfoService {
    /**
     * 根据摄像头ID获得区域内的节点信息
     * @param cameraId
     * @return
     */
    public AreaImageReq getNodeInfoByCameraId(String cameraId){
        QueryWrapper<CrmAreaNodeInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("camera_id",cameraId);
        List<CrmAreaNodeInfo> list = this.list(wrapper);
        AreaImageReq areaImageReq = new AreaImageReq();
        areaImageReq.setCameraId(cameraId);
        List<Punctuation> region = new ArrayList<>();
        for (CrmAreaNodeInfo nodeInfo : list) {
            Punctuation punctuation = new Punctuation();
            punctuation.setX(nodeInfo.getX());
            punctuation.setY(nodeInfo.getY());
            region.add(punctuation);
        }
        areaImageReq.setRegion(region);
        return areaImageReq;
    }

    public void saveAreaInfo(AreaImageReq areaImageReq){
        String cameraId = areaImageReq.getCameraId();
        QueryWrapper<CrmAreaNodeInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("camera_id",cameraId);
        List<CrmAreaNodeInfo> list = this.list(wrapper);
        if(list!=null && list.size()>0){
            this.remove(wrapper);
        }
        List<Punctuation> region = areaImageReq.getRegion();
        List<CrmAreaNodeInfo> nodeInfos = new ArrayList<>();
        for (Punctuation punctuation : region) {
            CrmAreaNodeInfo nodeInfo = new CrmAreaNodeInfo();
            nodeInfo.setCameraId(cameraId);
            nodeInfo.setX(punctuation.getX());
            nodeInfo.setY(punctuation.getY());
            nodeInfos.add(nodeInfo);
        }
        this.saveBatch(nodeInfos);
    }

    public String getImg(String cameraId) throws IOException {
        QueryWrapper<CrmAreaNodeInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("camera_id", cameraId);
        wrapper.isNotNull("img_sign");
        List<CrmAreaNodeInfo> list = this.list(wrapper);
        if(list.size() == 0) {
            return null;
        }else {
            return list.get(0).getImgSign();
        }
    }

    @Value("${early.warning.img_path}")
    public static final String IMG_PATH = "/";

    public void uploadImg(NodeInfoDto infoDto) throws Exception{
        String cameraId = infoDto.getCameraId();
        String base64Data = infoDto.getBase64Data();
        QueryWrapper<CrmAreaNodeInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("camera_id", cameraId);
        List<CrmAreaNodeInfo> list = this.list(wrapper);
        //保存到数据库
        if(list.size()>0) {
            CrmAreaNodeInfo nodeInfo = list.get(0);
            nodeInfo.setImgSign(base64Data);
            this.saveOrUpdate(nodeInfo);
        }
        if("0".equals(infoDto.getAppScenarios())) {
            byte[] bytes = Base64.getDecoder().decode(infoDto.getBase64Data());
            String uuid = UUID.randomUUID().toString();
            FileUtils.writeByteArrayToFile(new File(IMG_PATH + DateFormatUtils.format(new Date(),"yyyy_MM_dd") +"face/"+ uuid +"_"+cameraId + ".jpg"),bytes);
        }else if("1".equals(infoDto.getAppScenarios())) {
            byte[] bytes = Base64.getDecoder().decode(infoDto.getBase64Data());
            String uuid = UUID.randomUUID().toString();
            FileUtils.writeByteArrayToFile(new File(IMG_PATH + DateFormatUtils.format(new Date(),"yyyy_MM_dd") +"/security/"+ uuid +"_"+cameraId + ".jpg"),bytes);
        }else {
            //todo 后面设置识别类型表
        }
    }
}
